package cn.vcjf.weapp.common;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Auther: zhengzihang
 * @Date: 2018/10/9 18:29
 * @Description:
 */
public class FilterCommon implements Filter {
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

	}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
		// 对request、response进行一些预处理
//		servletRequest.setCharacterEncoding("UTF-8");
//		servletResponse.setCharacterEncoding("UTF-8");
//		servletResponse.setContentType("text/html;charset=UTF-8");

		System.out.println("----调用service之前执行一段代码----");
		if(servletRequest instanceof HttpServletRequest){
			servletRequest = (HttpServletRequest) servletRequest;
			String sysUserId = ((HttpServletRequest) servletRequest).getHeader("sysUserId");
			servletRequest.setAttribute("sysUserId",sysUserId);
		}

		filterChain.doFilter(servletRequest, servletResponse); // 执行目标资源，放行
		System.out.println("----调用service之后执行一段代码----");
	}

	@Override
	public void destroy() {

	}
}
